<template>
    <div>
        <h3 class="title">应收保证金管理</h3>
        <vxe-grid ref='xGrid' v-bind="gridOptions">
            <template #toolbar_buttons>
                <div class="tools">
                    <vxe-button @click="insertEvent">重置</vxe-button>
                    <vxe-button @click="insertEvent" status="primary">查询</vxe-button>
                </div>
            </template>
        </vxe-grid>
    </div>

</template>

<script>

import { mapState } from 'vuex'
import XEUtils from 'xe-utils'
import VXETable from 'vxe-table'

export default {
    data() {
        return {
            gridOptions: {
                border: true,
                showHeaderOverflow: true,
                showOverflow: true,
                keepSource: true,
                id: 'full_edit_1',
                height: 600,
                rowId: 'id',
                rowConfig: {
                    isHover: true
                },
                columnConfig: {
                    resizable: true
                },
                customConfig: {
                    storage: true,
                    checkMethod: this.checkColumnMethod
                },
                printConfig: {
                    columns: [
                        { field: 'name' },
                        { field: 'email' },
                        { field: 'nickname' },
                        { field: 'age' },
                        { field: 'amount' }
                    ]
                },
                sortConfig: {
                    trigger: 'cell',
                    remote: true
                },
                filterConfig: {
                    remote: true
                },
                pagerConfig: {
                    pageSize: 10,
                    pageSizes: [5, 10, 15, 20, 50, 100, 200, 500, 1000]
                },
                formConfig: {
                    titleWidth: 100,
                    titleAlign: 'right',
                    items: [
                        { field: 'name', title: '姓名', span: 20, titlePrefix: { message: 'app.body.valid.rName', icon: 'vxe-icon-info-circle-fill' }, itemRender: { name: '$input', props: { placeholder: '请输入名称' } } },
                        { field: 'email', title: '邮件', span: 20, titlePrefix: { useHTML: true, message: '点击链接：<a class="link" href="https://vxetable.cn" target="_blank">vxe-table官网</a>', icon: 'vxe-icon-question-circle-fill' }, itemRender: { name: '$input', props: { placeholder: '请输入邮件' } } },
                        { field: 'nickname', title: '昵称', span: 20, itemRender: { name: '$input', props: { placeholder: '请输入昵称' } } },
                        { field: 'role', title: '角色', span: 20, folding: true, itemRender: { name: '$input', props: { placeholder: '请输入角色' } } },
                        { field: 'sex', title: '性别', span: 20, folding: true, titleSuffix: { message: '注意，必填信息！', icon: 'vxe-icon-info-circle-fill' }, itemRender: { name: '$select', options: [] } },
                        { field: 'age', title: '年龄', span: 20, folding: true, itemRender: { name: '$input', props: { type: 'number', min: 1, max: 120, placeholder: '请输入年龄' } } },
                        { span: 12, align: 'center', collapseNode: true, }
                    ]
                },
                // toolbarConfig: {
                //     buttons: [
                //         { code: 'insert_actived', span: 20, name: '新增', icon: 'vxe-icon-square-plus' },
                //         { code: 'delete', span: 20, name: '直接删除', icon: 'vxe-icon-delete' },
                //         { code: 'mark_cancel', span: 20, name: '删除/取消', icon: 'vxe-icon-delete' },
                //         { code: 'save', span: 20, name: 'app.body.button.save', icon: 'vxe-icon-save', status: 'success' }
                //     ],
                //     refresh: true,
                //     import: true,
                //     export: true,
                //     print: true,
                //     zoom: true,
                //     custom: true
                // },
                toolbarConfig: {
                    // custom: true,
                    buttons: [
                        { code: 'myInsert', name: '新增' },
                    ],
                    slots: {
                        buttons: 'toolbar_buttons'
                    }
                },

                proxyConfig: {
                    seq: true, // 启用动态序号代理，每一页的序号会根据当前页数变化
                    sort: true, // 启用排序代理，当点击排序时会自动触发 query 行为
                    filter: true, // 启用筛选代理，当点击筛选时会自动触发 query 行为
                    form: true, // 启用表单代理，当点击表单提交按钮时会自动触发 reload 行为
                    // 对应响应结果 { result: [], page: { total: 100 } }
                    props: {
                        result: 'result', // 配置响应结果列表字段
                        total: 'page.total' // 配置响应结果总页数字段
                    },
                    // 只接收Promise，具体实现自由发挥
                    ajax: {
                        // 当点击工具栏查询按钮或者手动提交指令 query或reload 时会被触发
                        query: ({ page, sorts, filters, form }) => {
                            const queryParams = Object.assign({}, form)
                            // 处理排序条件
                            const firstSort = sorts[0]
                            if (firstSort) {
                                queryParams.sort = firstSort.property
                                queryParams.order = firstSort.order
                            }
                            // 处理筛选条件
                            filters.forEach(({ property, values }) => {
                                queryParams[property] = values.join(',')
                            })
                            return fetch(`${this.serveApiUrl}/api/pub/page/list/${page.pageSize}/${page.currentPage}`, queryParams).then(response => response.json())
                        },
                        // 当点击工具栏删除按钮或者手动提交指令 delete 时会被触发
                        delete: ({ body }) => {
                            return fetch(`${this.serveApiUrl}/api/pub/save`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }).then(response => response.json())
                        },
                        // 当点击工具栏保存按钮或者手动提交指令 save 时会被触发
                        save: ({ body }) => {
                            return fetch(`${this.serveApiUrl}/api/pub/save`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }).then(response => response.json())
                        }
                    }
                },
                columns: [
                    { type: 'checkbox', title: '序号', width: 120 },
                    { field: 'name', title: '姓名', width: 120, titlePrefix: { message: '名称必须填写！' }, },
                    {
                        field: 'role',
                        title: '角色',
                        width: 120,

                        titlePrefix: { useHTML: true, content: '点击链接：<a class="link" href="https://vxetable.cn" target="_blank">vxe-table官网</a>' },
                        filterMultiple: false,
                    },
                    { field: 'email', title: '邮箱', width: 120, },
                    { field: 'nickname', width: 120, title: '昵称', },
                    {
                        field: 'sex',
                        title: '性别',
                        width: 120,
                    },
                    { field: 'age', width: 120, title: '年龄', visible: false, },
                    { field: 'amount', width: 120, title: '金额', formatter: this.formatAmount, },
                    { field: 'updateDate', width: 120, title: '更新时间', width: 160, visible: false, formatter: this.formatDate },
                    { field: 'createDate', width: 120, title: '创建时间', width: 160, visible: false, formatter: this.formatDate }
                ],
                importConfig: {
                    remote: true,
                    importMethod: this.importMethod,
                    types: ['xlsx'],
                    modes: ['insert']
                },
                exportConfig: {
                    remote: true,
                    exportMethod: this.exportMethod,
                    types: ['xlsx'],
                    modes: ['current', 'selected', 'all']
                },
                checkboxConfig: {
                    labelField: 'id',
                    reserve: true,
                    highlight: true,
                    range: true
                },
                editRules: {
                    name: [
                        { required: true, message: 'app.body.valid.rName' },
                        { min: 3, max: 50, message: '名称长度在 3 到 50 个字符' }
                    ],
                    email: [
                        { required: true, message: '邮件必须填写' }
                    ],
                    role: [
                        { required: true, message: '角色必须填写' }
                    ]
                },
                editConfig: {
                    trigger: 'click',
                    mode: 'row',
                    showStatus: true
                }
            }
        }
    },
    computed: {
        ...mapState([
            'serveApiUrl'
        ])
    },
    created() {
        this.findSexList()
    },
    methods: {
        findSexList() {
            setTimeout(() => {
                const sexList = [
                    { label: '', value: '' },
                    { label: '男', value: '1' },
                    { label: '女', value: '0' }
                ]
                // 异步更新下拉选项
                this.sexList = sexList
                const $grid = this.$refs.xGrid
                if ($grid) {
                    const sexColumn = $grid.getColumnByField('sex')
                    sexColumn.editRender.options = sexList
                    const sexItem = $grid.getFormItems(4)
                    sexItem.itemRender.options = sexList
                }
            }, 100)
        },
        formatAmount({ cellValue }) {
            return cellValue ? `￥${XEUtils.commafy(XEUtils.toNumber(cellValue), { digits: 2 })}` : ''
        },
        formatDate({ cellValue }) {
            return XEUtils.toDateString(cellValue, 'yyyy-MM-dd HH:ss:mm')
        },
        checkColumnMethod({ column }) {
            if (['nickname', 'role'].includes(column.property)) {
                return false
            }
            return true
        },
        // 自定义服务端导入
        importMethod({ file }) {
            // 处理表单
            const formBody = new FormData()
            formBody.append('file', file)
            // 上传文件
            return fetch(`${this.serveApiUrl}/api/pub/import`, { method: 'POST', body: formBody }).then(response => response.join()).then(data => {
                const $grid = this.$refs.xGrid
                VXETable.modal.message({ content: `成功导入 ${data.result.insertRows} 条记录！`, status: 'success' })
                // 导入完成，刷新表格
                $grid.commitProxy('query')
            }).catch(() => {
                VXETable.modal.message({ content: '导入失败，请检查数据是否正确！', status: 'error' })
            })
        },
        // 自定义服务端导出
        exportMethod({ options }) {
            const $grid = this.$refs.xGrid
            const proxyInfo = $grid.getProxyInfo()
            // 传给服务端的参数
            const body = {
                filename: options.filename,
                sheetName: options.sheetName,
                isHeader: options.isHeader,
                original: options.original,
                mode: options.mode,
                pager: proxyInfo.pager,
                ids: options.mode === 'selected' ? options.data.map(item => item.id) : [],
                fields: options.columns.map(column => {
                    return {
                        field: column.property,
                        title: column.title
                    }
                })
            }
            // 开始服务端导出
            return fetch(`${this.serveApiUrl}/api/pub/export`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }).then(response => response.json()).then(data => {
                if (data.id) {
                    VXETable.modal.message({ content: '导出成功，开始下载', status: 'success' })
                    // 读取路径，请求文件
                    fetch(`${this.serveApiUrl}/api/pub/export/download/${data.id}`)
                        .then(response => response.blob())
                        .then(blob => {
                            // 开始下载
                            VXETable.saveFile({ filename: '导出数据', type: 'xlsx', content: blob })
                        })
                }
            }).catch(() => {
                VXETable.modal.message({ content: '导出失败！', status: 'error' })
            })
        }
    }
}
</script> 

<style scoped lang="scss">
.tools {
    display: flex;
    justify-content: center;
}

::v-deep .vxe-pager--total {
    display: none;
}

::v-deep .vxe-table--main-wrapper {
    height: 40vh;

    .vxe-table--body-wrapper {
        height: 100% !important;
    }
}

.title {
    // text-align: center;
    padding: 10px
}
</style>
